package com.system.borrow.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.system.borrow.Constant.URLDefind;
import com.system.borrow.R;
import com.system.borrow.adapter.MsgAdapter;
import com.system.borrow.bean.Action;
import com.system.borrow.bean.GloData;
import com.system.borrow.cache.CacheMode;
import com.system.borrow.callback.DialogCallback;
import com.system.borrow.http.utils.OkHttpUtils;
import com.system.borrow.pull.XListView;
import com.system.borrow.utils.SecondTitle;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Request;
import okhttp3.Response;

public class ActionActivity extends BaseActivity implements XListView.IXListViewListener{
    XListView lv_data;
    MsgAdapter adapter;
    int pageNo = 1;
    List<Action> datas = new ArrayList<>();
    Handler mHandler = new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_action);
        SecondTitle title = new SecondTitle(this);
        title.setTitle("活动推荐",null);
        initView();
        addAction();
        loadData();
        initListener();
    }
    private void initListener() {
//        lv_data.setRefreshEnable(true);
//        lv_data.setLoadEnable(true);
//        lv_data.setAutoLoadEnable(false);
//        lv_data.setListViewPlusListener(this);
        lv_data.setXListViewListener(this);
        lv_data.setPullLoadEnable(true);
        lv_data.setPullRefreshEnable(true);
        lv_data.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(GloData.getCustomerDTO()==null){
                    Intent loginIntent = new Intent(ActionActivity.this,LoginActivity.class);
                    startActivity(loginIntent);
                    return;
                }
                if(position==0){
                    return;
                }
                Action action = (Action) adapter.getItem(position-1);
                if(action==null){
                    return;
                }
                Intent intent = new Intent(ActionActivity.this, WebViewActivity.class);
                intent.putExtra("name",action.title);
                intent.putExtra("url",action.url);
                intent.putExtra("id",action.id);
                intent.putExtra("type",1);
                startActivity(intent);
            }
        });
    }
    private void loadData() {
        OkHttpUtils.get(URLDefind.ACTION_LIST)
                .tag(this)
                .params("pageNo",pageNo+"")
                .params("pageSize","10")
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .execute(new DialogCallback<String>(ActionActivity.this,String.class,null) {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        lv_data.stopRefresh();
                        lv_data.stopLoadMore();
                        if(s!=null){
                            try {
                                JSONObject json = new JSONObject(s);
                                int statusCode = json.getInt("statusCode");
                                if(statusCode==200){
                                    if(pageNo==1&&datas.size()>0){
                                        datas = new ArrayList<Action>();
                                    }
                                    Gson gson = new Gson();
                                    TypeToken<List<Action>> token = new TypeToken<List<Action>>(){};
                                    List<Action> list = gson.fromJson(json.getJSONArray("data").toString(),token.getType());
                                    if(list==null || list.size()==0){
                                        ToastShow("暂无更多数据");
                                    }else{
                                        datas.addAll(list);
                                        mHandler.post(new Runnable() {
                                            @Override
                                            public void run() {
                                                adapter.notifyDataSetChanged();

                                            }
                                        });
                                    }
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                    @Override
                    public void onCacheResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        if(s!=null){
                            try {
                                JSONObject json = new JSONObject(s);
                                int statusCode = json.getInt("statusCode");
                                if(statusCode==200&& pageNo==1){
                                    Gson gson = new Gson();
                                    TypeToken<List<Action>> token = new TypeToken<List<Action>>(){};
                                    List<Action> list = gson.fromJson(json.getJSONArray("data").toString(),token.getType());
                                        datas.addAll(list);
                                    mHandler.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            adapter.notifyDataSetChanged();
                                        }
                                    });
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
    }

    private void addAction() {
        adapter = new MsgAdapter(this,datas);
        adapter.setType(1);
        lv_data.setAdapter(adapter);
    }

    private void initView() {
        lv_data = (XListView) findViewById(R.id.lv_data);
    }



    @Override
    public void onRefresh() {
        pageNo = 1;
        datas = new ArrayList<>();
        loadData();
    }

    @Override
    public void onLoadMore() {
        pageNo ++;
        loadData();
    }
}
